package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.PlaceAdapter;
import com.example.xianji.adapter.ZhanHui_Adapter;
import com.example.xianji.BGARefresh.BGANormalRefreshViewHolder;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.Place;
import com.example.xianji.bean.ZhanHui_Bean;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_ZhanHui_DaTingActivity extends Activity implements
        OnClickListener, BGARefreshLayoutDelegate {
    public static String text_zhanhui_context = "";// 展会大厅搜索的关键词
    /************************ 展会大厅页面 ***************************************/
    private RelativeLayout zhanhui_relative_back;
    private BGARefreshLayout zhanhui_RefreshLayout;
    private ListView zhanhui_mylistview, zhanhui_fenlei_liebiao;
    private int cid = 0;
    private int page = 1;
    private ZhanHui_Adapter mZhanHui_Adapter;// 展会大厅列表适配器
    private View translation_view_zhanhui;
    private ArrayList<ZhanHui_Bean> list = new ArrayList<ZhanHui_Bean>();
    private ArrayList<Place> fenlei_list = new ArrayList<Place>();
    private PlaceAdapter mPlaceAdapter;
    private LinearLayout linear_layout_zhanhui_fenlei;
    private TextView text_zhanhui_fenlei, zhanhui_dating_title;
    private ImageView zhanhui_search;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                zhanhui_mylistview.setAdapter(mZhanHui_Adapter);
                zhanhui_RefreshLayout.endRefreshing();
            }
            if (msg.what == 2) {
                mZhanHui_Adapter.notifyDataSetChanged();
                zhanhui_RefreshLayout.endLoadingMore();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__zhan_hui__da_ting);
        init();
        zhanhui_RefreshLayout
                .setRefreshViewHolder(new BGANormalRefreshViewHolder(this, true));
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                zhanhui_data();
            }
        });

        zhanhui_mylistview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(Main_ZhanHui_DaTingActivity.this,
                        Main_Zhanhui_DetailsActivity.class);
                intent.putExtra("zhanhui_id", list.get(position).getId());
                intent.putExtra("zhanhui_title", list.get(position).getTitle());
                Main_ZhanHui_DaTingActivity.this.startActivity(intent);
            }
        });
        zhanhui_fenlei_liebiao
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        translation_view_zhanhui.setVisibility(View.GONE);
                        zhanhui_fenlei_liebiao.setVisibility(View.GONE);
                        cid = Integer.parseInt(fenlei_list.get(position)
                                .getArea_id());
                        text_zhanhui_fenlei.setText(fenlei_list.get(position)
                                .getArea_name());
                        zhanhui_data();
                    }
                });

    }

    private void init() {
        zhanhui_relative_back = (RelativeLayout) findViewById(R.id.zhanhui_relative_back);
        zhanhui_RefreshLayout = (BGARefreshLayout) findViewById(R.id.zhanhui_RefreshLayout);
        zhanhui_mylistview = (ListView) findViewById(R.id.zhanhui_mylistview);
        zhanhui_fenlei_liebiao = (ListView) findViewById(R.id.zhanhui_fenlei_liebiao);
        translation_view_zhanhui = (View) findViewById(R.id.translation_view_zhanhui);
        linear_layout_zhanhui_fenlei = (LinearLayout) findViewById(R.id.linear_layout_zhanhui_fenlei);
        text_zhanhui_fenlei = (TextView) findViewById(R.id.text_zhanhui_fenlei);
        mZhanHui_Adapter = new ZhanHui_Adapter(list, this);
        mPlaceAdapter = new PlaceAdapter(this, fenlei_list, 2);
        zhanhui_search = (ImageView) findViewById(R.id.zhanhui_search);
        zhanhui_dating_title = (TextView) findViewById(R.id.zhanhui_dating_title);

        zhanhui_relative_back.setOnClickListener(this);
        linear_layout_zhanhui_fenlei.setOnClickListener(this);
        translation_view_zhanhui.setOnClickListener(this);
        zhanhui_search.setOnClickListener(this);
        zhanhui_RefreshLayout.setDelegate(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.zhanhui_relative_back) {
            Main_ZhanHui_DaTingActivity.this.finish();
            text_zhanhui_context = "";
        } else if (v.getId() == R.id.linear_layout_zhanhui_fenlei) {
            zhanhui_fenlei_liebiao.setVisibility(View.VISIBLE);
            translation_view_zhanhui.setVisibility(View.VISIBLE);
            zhanhui_fenlei_liebiao.setAdapter(mPlaceAdapter);
        } else if (v.getId() == R.id.translation_view_zhanhui) {
            translation_view_zhanhui.setVisibility(View.GONE);
            zhanhui_fenlei_liebiao.setVisibility(View.GONE);

        } else if (v.getId() == R.id.zhanhui_search) {
            Intent intent = new Intent(Main_ZhanHui_DaTingActivity.this,
                    Main_SearchActivity.class);
            intent.putExtra("search", 8);
            startActivity(intent);
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        zhanhui_data();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(
            BGARefreshLayout refreshLayout) {
        zhanhui_data_more();
        return true;
    }

    public void zhanhui_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();

        parms.addBodyParameter("cid", cid + "");
        parms.addBodyParameter("page", 1 + "");

        parms.addBodyParameter("k", text_zhanhui_context);
        httpUtils.send(HttpMethod.POST, Contant.zhanhui_list, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        zhanhui_RefreshLayout.endRefreshing();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            if (object.getInt("status") == 1) {
                                list.clear();
                                JSONArray array = object.getJSONArray("data");
                                // 列表内容
                                JSONArray array0 = array.getJSONArray(0);
                                // 列表分类
                                JSONArray array1 = array.getJSONArray(1);

                                Log.i("bbbbbbbbbbb", array1 + "");
                                for (int i = 0; i < array0.length(); i++) {
                                    JSONObject OBJ = array0.getJSONObject(i);
                                    ZhanHui_Bean zhanhui = new ZhanHui_Bean();
                                    zhanhui.setArea_id(OBJ.getInt("area_id"));
                                    zhanhui.setDate(OBJ.getString("zdate"));
                                    zhanhui.setId(OBJ.getInt("id"));
                                    zhanhui.setImge(OBJ.getString("zimg"));
                                    zhanhui.setPlace(OBJ.getString("zplace"));
                                    zhanhui.setTitle(OBJ.getString("ztitle"));
                                    list.add(zhanhui);

                                }
                                for (int i = 0; i < array1.length(); i++) {
                                    JSONObject fenlei_object = array1
                                            .getJSONObject(i);

                                    Place mPlace = new Place();
                                    mPlace.setArea_id(fenlei_object
                                            .getInt("id") + "");
                                    mPlace.setArea_name(fenlei_object
                                            .getString("tag_name"));
                                    fenlei_list.add(mPlace);

                                }
                                handler.sendEmptyMessage(1);

                            } else {
                                zhanhui_RefreshLayout.endRefreshing();
                                Toast.makeText(Main_ZhanHui_DaTingActivity.this,
                                        object.getString("msg"), 0).show();
                            }
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }

                    }
                });
    }

    public void zhanhui_data_more() {
        page++;
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();

        parms.addBodyParameter("cid", cid + "");
        parms.addBodyParameter("page", page + "");

        parms.addBodyParameter("k", "");
        httpUtils.send(HttpMethod.POST, Contant.zhanhui_list, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            if (object.getInt("status") == 1) {
                                JSONArray array = object.getJSONArray("data");
                                // 列表内容
                                JSONArray array0 = array.getJSONArray(0);
                                // 列表分类
                                JSONArray array1 = array.getJSONArray(1);
                                if (array0.length() == 0) {
                                    zhanhui_RefreshLayout.endLoadingMore();
                                    Toast.makeText(
                                            Main_ZhanHui_DaTingActivity.this,
                                            "没有更多数据哦", 0).show();
                                } else {
                                    for (int i = 0; i < array0.length(); i++) {
                                        JSONObject OBJ = array0
                                                .getJSONObject(i);
                                        ZhanHui_Bean zhanhui = new ZhanHui_Bean();
                                        zhanhui.setArea_id(OBJ
                                                .getInt("area_id"));
                                        zhanhui.setDate(OBJ.getString("zdate"));
                                        zhanhui.setId(OBJ.getInt("id"));
                                        zhanhui.setImge(OBJ.getString("zimg"));
                                        zhanhui.setPlace(OBJ
                                                .getString("zplace"));
                                        zhanhui.setTitle(OBJ
                                                .getString("ztitle"));
                                        list.add(zhanhui);

                                    }
                                    handler.sendEmptyMessage(2);
                                }
                            } else {
                                zhanhui_RefreshLayout.endLoadingMore();
                                Toast.makeText(
                                        Main_ZhanHui_DaTingActivity.this,
                                        object.getString("msg"), 0).show();
                            }

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }

                    }
                });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Main_SearchActivity.zhanhui_id == 1) {
            zhanhui_dating_title.setText(text_zhanhui_context);
            zhanhui_data();
            Main_SearchActivity.zhanhui_id = 0;
        }
    }
}
